package P3;

import java.util.concurrent.*;

/**
 * 基于Future、Callable和Executor查找数组中的最大值。
 * Int[] data = {2, 8, 1, 6, 10, 9, 5, 3, 25, 0};
 */
public class MaxValueFinder {
    public static void main(String[] args) {
        Integer[] data = {2, 8, 1, 6, 10, 9, 5, 3, 25, 0};
        int numThreads = 4; // 可以根据需要调整线程数量

        ExecutorService executor = Executors.newFixedThreadPool(numThreads);
        Callable<Integer> task = new MaxValueTask(data);

        Future<Integer> future = executor.submit(task);

        try {
            int maxValue = future.get();
            System.out.println("The maximum value in the array is: " + maxValue);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

        executor.shutdown();
    }

    private static class MaxValueTask implements Callable<Integer> {
        private final Integer[] data;

        public MaxValueTask(Integer[] data) {
            this.data = data;
        }

        @Override
        public Integer call() {
            int max = Integer.MIN_VALUE;
            for (Integer num : data) {
                if (num > max) {
                    max = num;
                }
            }
            return max;
        }
    }
}